﻿using System;
using System.Web.UI.WebControls;
using EOSCRM.Domain;
using EOSCRM.Web.Common;
using EOSCRM.Util;
using EOSCRM.Dao;
using EOSCRM.Web.Shared;
using Microsoft.Reporting.WebForms;
using System.Web.UI.HtmlControls;

namespace EOSCRM.Web.Forms.BaoCao.Luong
{
    public partial class BangKeTienLuongUngTruoc : Authentication
    {
        private NhanVienDao _nvdao = new NhanVienDao();   
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                    LoadReferences();
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }
        private void LoadReferences()
        {
            txtNgayLap.Text = DateTime.Now.ToString("dd/MM/yyyy");
            CommonFunc.BindDropDownList(ddlKhuVuc, new KhuVucDao().GetList(), "TenKhuVuc", "MaKhuVuc", true);
            CommonFunc.BindDropDownList(ddlLo, new LoDao().GetListKhuVuc(ddlKhuVuc.SelectedValue), "TenLo", "MaLo", true);
            txtTuNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtDenNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
            var nvlist = _nvdao.GetListLo(ddlLo.SelectedValue);
            CommonFunc.BindDropDownList(ddlNhanVien, nvlist, "TenNV", "MaNV", true);
        }
        protected void ddlKhuVuc_Change(object sender, EventArgs e)
        {
            CommonFunc.BindDropDownList(ddlLo, new LoDao().GetListKhuVuc(ddlKhuVuc.SelectedValue), "TenLo", "MaLo", true);
            upnlInfor.Update();
        }
        protected void ddlLo_Change(object sender, EventArgs e)
        {
            var nvlist = _nvdao.GetListLo(ddlLo.SelectedValue);
            CommonFunc.BindDropDownList(ddlNhanVien, nvlist, "TenNV", "MaNV", true);
        }
        protected void btnReport_Click(object sender, EventArgs e)
        {

            var myds =
               new ReportClass().L_BangKeTienLuongUngTruoc(ddlKhuVuc.SelectedValue, ddlLo.SelectedValue,ddlNhanVien.SelectedValue,
               ConvertUtil.ToDateTime(txtTuNgay.Text.Trim()).Value,
               ConvertUtil.ToDateTime(txtDenNgay.Text.Trim()).Value);
            if (myds == null || myds.Tables.Count == 0) { CloseWaitingDialog(); return; }

            ReportDataSource ds = new ReportDataSource("dsLuongUngTruoc", myds.Tables[0]);

            rpViewer.LocalReport.DataSources.Clear();
            rpViewer.LocalReport.ReportPath = "Reports/Luong/TienUngTruoc.rdlc";
            rpViewer.LocalReport.DataSources.Add(ds);
            ReportParameter rpNgayLap = new ReportParameter("rpNgayThangNam", NgayThangNam(txtNgayLap));
            ReportParameter rpTuNgayDenNgay = new ReportParameter("rpTuNgayDenNgay", String.Format("Từ ngày {0} đến ngày {1}", txtTuNgay.Text.Trim(), txtDenNgay.Text.Trim()));
            rpViewer.LocalReport.SetParameters(new ReportParameter[] { rpNgayLap,rpTuNgayDenNgay });

            rpViewer.LocalReport.Refresh();

            divCR.Visible = true;
            upnlReport.Update();
            CloseWaitingDialog();
        }

        
    }
}